<!--
 * @Author: YanMeng
 * @Date: 2022-03-03 13:18:23
 * @LastEditors: YanMeng
 * @LastEditTime: 2022-10-20 09:22:53
 * @Description: file content
 * @FilePath: \ias_ui\src\views\coi\spuManagement\components\wirteSpuName.vue
 * @Company: 沃尔玛
-->
<template>
  <el-dialog class="wirte-spu-name" :visible.sync="dialogVisible" width="500px" append-to-body :before-close="handleClose">
    <el-form ref="form" :model="formData" :rules="rules" label-width="auto" size="mini">
      <el-form-item label="SPU名称" prop="spuName">
        <el-input v-model="formData.spuName" maxlength="45" show-word-limit />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" maxlength="255" show-word-limit />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleClose">取 消</el-button>
      <el-button type="primary" size="mini" :loading="loading" @click="submitSendSpu">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { submitSpuSend } from '@/api/coi/onlineTaskAPI/spu.js'

export default {
  name: 'WirteSpuName',
  props: {
    formData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      rules: {
        spuName: [
          { required: true, message: '请输入SPU名称', trigger: 'blur' },
          { max: 45, message: 'SPU名字在45个字符之内', trigger: 'blur' }
        ],
        remark: [{ max: 255, message: '备注在255个字符之内', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    submitSendSpu() {
      this.$refs.form.validate(async (v) => {
        if (!v) return
        this.submitSendSpuRequest()
      })
    },
    async submitSendSpuRequest() {
      try {
        this.loading = true
        const { result } = await submitSpuSend(this.formData)
        if (result) {
          this.formData.spuId = result
          this.$emit('sendSpuSuccess')
          this.handleClose()
        }
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.wirte-spu-name /deep/ .el-dialog__body {
  padding-top: 20px;
  height: auto !important;
}
</style>
